package com.bgyrobot.coordinate.test.util;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.bgyrobot.coordinate.ICoordinateSystemProvider;
import com.bgyrobot.coordinate.model.CoordinateSystem;

public class JsonCSProvider implements ICoordinateSystemProvider {
    
    private HashMap<String, CoordinateSystem> dict;
    
    public JsonCSProvider(String path) {
        
        try {
            String fileContent = TestUtil.readFile(new File(path));
            List<CoordinateSystem> csList = JSON.parseArray(fileContent, CoordinateSystem.class);
            dict = new HashMap<String, CoordinateSystem>();
            for (CoordinateSystem cs : csList) {
                dict.put(cs.getName(), cs);
            }
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public CoordinateSystem query(String name) {
        return dict.get(name);
    }

    public void updatesert(CoordinateSystem coordinateSystem) {
        dict.put(coordinateSystem.getName(), coordinateSystem);

    }

    public void delete(String name) {
        dict.remove(name);

    }
}
